/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaModificarPersonaje.java
 *
 * Created on 14-may-2012, 12:15:02
 */
package gui;

import ClasesAModificar.PersonajeMoficar;
import javax.swing.JOptionPane;
import src.BorraCeldas;
import src.FuncionesModificar;

/**
 *
 * @author usuario_local
 */
public class VentanaModificarPersonaje extends javax.swing.JFrame {

    int elem;
    VentanaModificar vm;
    
    /** Creates new form VentanaModificarPersonaje */
    public VentanaModificarPersonaje(int elem, VentanaModificar vm, PersonajeMoficar PM) {
        this.vm = vm;
        this.elem = elem;
        initComponents();
        inicializarContenido(PM);
        this.PM = PM;
    }

    private void inicializarContenido(PersonajeMoficar PM)
    {
        jTextField_vpBiografia.setText(PM.getBiografia());
        jTextField_vpFechaDefun.setText(String.valueOf(PM.getFDef()));
        jTextField_vpFechaNaci.setText(String.valueOf(PM.getFNac()));
        jTextField_vpLugarDefun.setText(PM.getLDef());
        jTextField_vpLugarNaci.setText(PM.getLNac());
        jTextField_vpNombApe.setText(PM.getNombreYApellidos());
    }
    
        private boolean comprobar(){
        String mensaje = "";
        boolean resultado = false;
        
        if (this.jTextField_vpNombApe.getText().equals(""))
            mensaje += "Por favor rellene el campo de nombre y apellidos\n\n";
        if (this.jTextField_vpLugarNaci.getText().equals(""))
            mensaje += "Por facor rellene el campo de lugar de nacimiento\n\n";
        if (this.jTextField_vpLugarNaci.getText().equals(""))
            mensaje += "Por facor rellene el campo de lugar de nacimiento\n\n";
        if (this.jTextField_vpBiografia.getText().equals(""))
            mensaje += "Por favor rellene el campo de biografía\n\n";
        if(!mensaje.equals(""))
            JOptionPane.showMessageDialog(null, mensaje);
        else
            resultado = true;
        
        try{
            Integer.parseInt(this.jTextField_vpFechaNaci.getText());
            Integer.parseInt(this.jTextField_vpFechaDefun.getText());
            if(this.jTextField_vpFechaNaci.getText().length() != 8 || this.jTextField_vpFechaDefun.getText().length() != 8)
                throw new Exception();
            
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Por favor rellene las fechas de la siguiente manera:\n"
                    + "ej. 20120101 siendo los 4 primeros numeros el año, los dos siguientes el mes\n"
                    + "y los dos ultimos el día\n\n");
            resultado = false;
        }
              
        return resultado;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel_vpNombApe = new javax.swing.JLabel();
        jTextField_vpNombApe = new javax.swing.JTextField();
        jLabel_vpFechaNaci = new javax.swing.JLabel();
        jTextField_vpFechaNaci = new javax.swing.JTextField();
        jLabel_vpLugarNaci = new javax.swing.JLabel();
        jTextField_vpLugarNaci = new javax.swing.JTextField();
        jLabel_vpFechaDefun = new javax.swing.JLabel();
        jTextField_vpFechaDefun = new javax.swing.JTextField();
        jLabel_vpLugarDefun = new javax.swing.JLabel();
        jTextField_vpLugarDefun = new javax.swing.JTextField();
        jLabel_vpBiografia = new javax.swing.JLabel();
        jTextField_vpBiografia = new javax.swing.JTextField();
        jButton_VMPalias = new javax.swing.JButton();
        jButton_VMPCargo = new javax.swing.JButton();
        jButton_VMPTerminar = new javax.swing.JButton();
        jButton_VMPEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel_vpNombApe.setText("Nombre y Apellidos");

        jLabel_vpFechaNaci.setText("Fecha de Nacimiento");

        jLabel_vpLugarNaci.setText("Lugar de Nacimiento");

        jLabel_vpFechaDefun.setText("Fecha de Defuncion");

        jLabel_vpLugarDefun.setText("Lugar de Defuncion");

        jLabel_vpBiografia.setText("Biografia");

        jButton_VMPalias.setText("Rel. Alias");
        jButton_VMPalias.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_VMPaliasMouseClicked(evt);
            }
        });

        jButton_VMPCargo.setText("Rel. Cargo");
        jButton_VMPCargo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_VMPCargoMouseClicked(evt);
            }
        });

        jButton_VMPTerminar.setText("Terminar");
        jButton_VMPTerminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_VMPTerminarMouseClicked(evt);
            }
        });

        jButton_VMPEliminar.setText("Eliminar");
        jButton_VMPEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_VMPEliminarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpNombApe)
                    .addComponent(jLabel_vpFechaNaci)
                    .addComponent(jLabel_vpLugarNaci)
                    .addComponent(jLabel_vpFechaDefun)
                    .addComponent(jLabel_vpLugarDefun))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_vpFechaDefun, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(jTextField_vpLugarNaci, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(jTextField_vpFechaNaci, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(jTextField_vpNombApe, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(jTextField_vpLugarDefun, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel_vpBiografia)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField_vpBiografia, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jButton_VMPalias, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addComponent(jButton_VMPCargo, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(107, 107, 107)
                .addComponent(jButton_VMPEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_VMPTerminar)
                .addContainerGap(127, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpNombApe)
                    .addComponent(jTextField_vpNombApe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpFechaNaci)
                    .addComponent(jTextField_vpFechaNaci, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpLugarNaci)
                    .addComponent(jTextField_vpLugarNaci, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpFechaDefun)
                    .addComponent(jTextField_vpFechaDefun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_vpLugarDefun)
                    .addComponent(jTextField_vpLugarDefun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_vpBiografia)
                    .addComponent(jTextField_vpBiografia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_VMPalias)
                    .addComponent(jButton_VMPCargo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_VMPTerminar)
                    .addComponent(jButton_VMPEliminar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_VMPTerminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_VMPTerminarMouseClicked
        // TODO add your handling code here:
        if(comprobar())
        {
            vm.setVisible(true);
            this.setVisible(false);

            PM.setBiografia(jTextField_vpBiografia.getText());
            PM.setFDef(Integer.valueOf(jTextField_vpFechaDefun.getText()));
            PM.setFNac(Integer.valueOf(jTextField_vpFechaNaci.getText()));
            PM.setLDef(jTextField_vpLugarDefun.getText());
            PM.setLNac(jTextField_vpLugarNaci.getText());
            PM.setNombreYApellidos(jTextField_vpNombApe.getText());

            FuncionesModificar.modificarPersonaje(PM);
            this.dispose();
        }
    }//GEN-LAST:event_jButton_VMPTerminarMouseClicked

    private void jButton_VMPaliasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_VMPaliasMouseClicked
        // TODO add your handling code here:
        VentanaModificarRelaciones vmr = new VentanaModificarRelaciones(6,this,PM.getListaAlias(),PM.getIdPer());
        vmr.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton_VMPaliasMouseClicked

    private void jButton_VMPCargoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_VMPCargoMouseClicked
        // TODO add your handling code here:
        VentanaModificarRelaciones vmr = new VentanaModificarRelaciones(7,this,PM.getListaCargos(),PM.getIdPer());
        vmr.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton_VMPCargoMouseClicked

    private void jButton_VMPEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_VMPEliminarMouseClicked
        vm.setVisible(true);
        this.setVisible(false);
        BorraCeldas.BorrarPersonaje(PM.getIdPer());
        this.dispose();
    }//GEN-LAST:event_jButton_VMPEliminarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_VMPCargo;
    private javax.swing.JButton jButton_VMPEliminar;
    private javax.swing.JButton jButton_VMPTerminar;
    private javax.swing.JButton jButton_VMPalias;
    private javax.swing.JLabel jLabel_vpBiografia;
    private javax.swing.JLabel jLabel_vpFechaDefun;
    private javax.swing.JLabel jLabel_vpFechaNaci;
    private javax.swing.JLabel jLabel_vpLugarDefun;
    private javax.swing.JLabel jLabel_vpLugarNaci;
    private javax.swing.JLabel jLabel_vpNombApe;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField_vpBiografia;
    private javax.swing.JTextField jTextField_vpFechaDefun;
    private javax.swing.JTextField jTextField_vpFechaNaci;
    private javax.swing.JTextField jTextField_vpLugarDefun;
    private javax.swing.JTextField jTextField_vpLugarNaci;
    private javax.swing.JTextField jTextField_vpNombApe;
    // End of variables declaration//GEN-END:variables
    private PersonajeMoficar PM;
}
